#include<windows.h>

    HDC         hDC;
    PAINTSTRUCT Ps;
    HFONT	    font;
	HPEN        hPen;
	HPEN        res;
	HPEN        hcaut;

	char caut[20];
	char colorare[20];
	char inserare[100];
	char cautarebuf[100];
	int m=0;   // indice de cautare

	void nod_arbore(HDC hDC ,int x, int y,char *s){
		int z=x+80;
		int w=y+25;
		Rectangle(hDC,x,y,z,w);
		int dif=10-strlen(s);
		TextOut(hDC, x+5+3*dif, y+3, s , strlen(s));
}

	int coord[4][10]={{460},{110,345,580,815},{55,160,305,450,595,740,885},{55,160,305,450,595,740,885}};
	int level[4];

	void afisare(nod *&head, int nivel,int x,int y){
		if(strcmp(head->string,"*")==0)
		{
		//	nod_arbore(hDC,coord[nivel][level[nivel]],25+nivel*100,head->string);
		//	level[nivel]++;
			return;
		}

		if(strlen(head->string)>0)
		{
			SelectObject(hDC, hPen);
			SetTextColor(hDC,RGB(0,0,0));
			nod_arbore(hDC,coord[nivel][level[nivel]],25+nivel*100,head->string);
			if(head!=root)
			{
				MoveToEx(hDC, x, y, NULL);
				LineTo(hDC, coord[nivel][level[nivel]]+40,25+nivel*100);
			}
			level[nivel]++;
			afisare(head->sub[0],nivel+1,coord[nivel][level[nivel]-1]+40,50+nivel*100);
			afisare(head->sub[1],nivel+1,coord[nivel][level[nivel]-1]+40,50+nivel*100);
			afisare(head->sub[2],nivel+1,coord[nivel][level[nivel]-1]+40,50+nivel*100);
			afisare(head->sub[3],nivel+1,coord[nivel][level[nivel]-1]+40,50+nivel*100);
			afisare(head->sub[4],nivel+1,coord[nivel][level[nivel]-1]+40,50+nivel*100);
		}

}

	void resetare()
{
	for(int i=0;i<4;i++)
		level[i]=0;
}


	void colorare_cautare(nod *&head,char s[20],int nivel,int x,int y){

		if(strcmp(head->string,"*")==0)
			return;
		
		if(strlen(head->string)>0)
		{
			SetTextColor(hDC,RGB(225,0,0));
			SelectObject(hDC,hcaut);
			nod_arbore(hDC,coord[nivel][level[nivel]],25+nivel*100,head->string);
			if(head!=root)
			{
				MoveToEx(hDC, x, y, NULL);
				LineTo(hDC, coord[nivel][level[nivel]]+40,25+nivel*100);
			}
			strcpy(s,rest(head->string,s));
			level[nivel]++;
			for(int i=0;i<5;i++)
			{
				if(strlen(comparare(head->sub[i]->string,s))>0)
					colorare_cautare(head->sub[i],s,nivel+1,coord[nivel][level[nivel]-1]+40,50+nivel*100);
				else
					afisare(head->sub[i],nivel+1,coord[nivel][level[nivel]-1]+40,50+nivel*100);
	
			}
			
		}
}

	void free_node(nod *&head){
	
	if(strlen(head->string)>0)
	{
		free_node(head->sub[0]);
		free_node(head->sub[1]);
		free_node(head->sub[2]);
		free_node(head->sub[3]);
		free_node(head->sub[4]);
		free(head);
	}
}
